package com.qtyx.nms.mvp.presenter

import com.qtyx.nms.mvp.base.BaseObserver
import com.qtyx.nms.mvp.base.BasePresenter
import com.qtyx.nms.mvp.model.bean.*
import com.qtyx.nms.mvp.view.HomePageView
import com.qtyx.nms.util.shared.AppInfoShared

class HomePagePresenter(val view: HomePageView):BasePresenter() {

    /**一般护理 固定行数**/
    private val rowCountGeneral=5
    /**特殊护理 固定行数**/
    private val rowCountSpecial=6

    /**
     * 获取护理数据
     * @param type 1、一般护理，2、特殊护理
     * @param list 集合数据
     */
    private fun getNewList(type:Int,list:List<NurseBean.Data>):List<NurseBean.Data>{
        val rowCount=  if(type==1) rowCountGeneral else rowCountSpecial
        val spanCount=getSpanCount(type,list)
        val listNew= mutableListOf<NurseBean.Data>()
        listNew.addAll(list)
        //不足以铺满布局，添加空的对象
        if(list.size<rowCount*spanCount){
            val differ=rowCount*spanCount-list.size
            for (i in 1..differ){
                val data=NurseBean.Data("","","","", listOf(),"",0,0)
                listNew.add(data)
            }
        }
        return listNew
    }

    /**
     * 获取护理数据的列数
     * @param type 1、一般护理，2、特殊护理
     * @param list 集合数据
     */
    private fun getSpanCount(type:Int,list:List<NurseBean.Data>):Int{
        val rowCount=  if(type==1) rowCountGeneral else rowCountSpecial
        val maxSpanCount=  if(type==1) 3 else 4

        var spanCount =if (list.size % rowCount > 0)
                list.size / rowCount + 1
            else
                list.size / rowCount

        if(spanCount>maxSpanCount){
            spanCount=maxSpanCount
        }

        if(spanCount==0){
            spanCount=1
        }
        return spanCount
    }


    /**
     * 获取护理数据
     */
    fun getNurseData(){
        requestData(getApiServer().getNurseData("获取护理数据", AppInfoShared.getDeptId()), object : BaseObserver<NurseBean>(view,false) {
            override fun onSuccess(data: NurseBean){
                val listGeneral=data.data.filter { it.nursprogType=="1" }
                val listSpecial=data.data.filter { it.nursprogType=="2"||it.nursprogType=="3" }

                view.getGeneralNurseDataSucceed(getNewList(1,listGeneral),getSpanCount(1,listGeneral))
                view.getSpecialNurseDataSucceed(getNewList(2,listSpecial),getSpanCount(2,listSpecial))
            }
            override fun onError(msg: String)= view.onError(msg)
        })
    }
    /**
     * 获取护理等级颜色
     */
    fun getNurseGrade(){
        requestData(getApiServer().getNurseGrade("获取护理等级颜色"), object : BaseObserver<NurseGradeBean>(view,false) {
            override fun onSuccess(data: NurseGradeBean)=view.getNurseGradeSucceed(data.data)
            override fun onError(msg: String)= view.onError(msg)
        })
    }

    /**
     * 获取值班信息
     */
    fun getZhiBanInfo(){
        requestData(getApiServer().getZhiBanInfo("获取值班信息",AppInfoShared.getDeptId()), object : BaseObserver<ZhiBanBean>(view,false) {
            override fun onSuccess(data: ZhiBanBean)=view.getZhiBanInfoSucceed(data.data)
            override fun onError(msg: String)= view.onError(msg)
        })
    }
    /**
     * 获取消息列表
     */
    fun getMessageList(){
        requestData(getApiServer().getMessageList("获取消息列表",AppInfoShared.getDeptId()), object : BaseObserver<MessageBean>(view,false) {
            override fun onSuccess(data: MessageBean)=view.getMessageSucceed(data.data)
            override fun onError(msg: String)= view.onError(msg)
        })
    }
    /**
     * 获取医师列表
     */
    fun getYiShiList(){
        requestData(getApiServer().getYiShiList("获取医师列表",AppInfoShared.getDeptId()), object : BaseObserver<YiShiBean>(view,false) {
            override fun onSuccess(data: YiShiBean){
                val listNurse= mutableListOf<NurseBean.Data>()
                if(data.data.isNotEmpty()){
                    val map= mutableMapOf<String,MutableList<NurseBean.Data.SqlData>>()
                    for (datum in data.data) {

                        val sqlData=NurseBean.Data.SqlData(
                            datum.BED_NO,//床位号
                            datum.PATIENT_NAME,//病人姓名
                            datum.AGE,//年龄
                            datum.SEX,//性别
                            datum.DOCTOR_NAME,//主治医生
                            datum.NURSE_NAME,//责任护士
                            datum.CHARGE_TYPE,//收费种类
                            datum.BALANCE,//余额
                            datum.NURSE_LEVEL,//护理等级
                            datum.ADMISSION_DATE,
                            datum.BED_LABEL,
                            datum.DIAGNOSIS,
                            datum.INP_NO,
                            datum.MEAL,
                            datum.NATION
                        )
                        //主治医师ID
                        if(map[datum.DOCTOR_IN_CHARGE]==null){
                            val newList= mutableListOf<NurseBean.Data.SqlData>()
                            newList.add(sqlData)
                            map[datum.DOCTOR_IN_CHARGE]=newList
                        }else{
                            map[datum.DOCTOR_IN_CHARGE]?.add(sqlData)
                        }
                    }

                    for(values in map.values){
                        val yiShiInfo="${values[0].DOCTOR_NAME}　　共 ${values.size} 人"
                        val nurseData=NurseBean.Data(yiShiInfo,"",
                            "","",
                            values,values[0].DOCTOR_NAME,0,0)
                        listNurse.add(nurseData)
                    }
                }
                view.getYiShiSucceed(listNurse)
            }
            override fun onError(msg: String)= view.onError(msg)
        })
    }
}